package utils;

import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import aw.AlienWorldImpl;
import aw.Static_Variables;

public class ReadGameCfgXML {

	public static Object[] setupGameCfg() {

		ArrayList<ObjectMeaning> objectos = new ArrayList<ObjectMeaning>();
		ArrayList<ObjectCombines> combinacoes = new ArrayList<ObjectCombines>();

		try {
			File fXmlFile = new File("GameConfg.xml");
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(fXmlFile);

			doc.getDocumentElement().normalize();

			System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

			Node gameNode = doc.getDocumentElement();
			Static_Variables.turnTimeLimit = Integer.parseInt(gameNode.getAttributes().getNamedItem("turnTimeLimit").getNodeValue());
			AlienWorldImpl.NUMBER_OF_PLAYERS = Integer.parseInt(gameNode.getAttributes().getNamedItem("numberOfPlayers").getNodeValue());
			AlienWorldImpl.STATE_INITIAL = "0*"+Integer.parseInt(gameNode.getAttributes().getNamedItem("lifeOfAgent").getNodeValue());
			
			
			Node objects_ = gameNode.getChildNodes().item(1);
			NodeList objects = objects_.getChildNodes();
			
			System.out.println("____________________________dcadcd_____________________");
			for (int object_id = 0; object_id < objects.getLength(); object_id++) {

				Node nNode = objects.item(object_id);

				if (nNode.getNodeType() == Node.ELEMENT_NODE) {
					Element eElement = (Element) nNode;
						String id = eElement.getAttribute("id");
						String item = eElement.getAttribute("item");
						
						System.out.println("AQUI E K E,,,,,:::::"+id+" "+item);
						objectos.add(new ObjectMeaning(id, item));
				}
			}
			System.out.println("____________________________dcadcd_____________________");
			
			
			
			
			
			
			
			
			
			
			NodeList combines = doc.getElementsByTagName("combines");

			for (int temp = 0; temp < combines.getLength(); temp++) {
				
				Node nNode = combines.item(temp);
				
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {

					Element eElement = (Element) nNode;
					NodeList doors = eElement.getElementsByTagName("object");
					
					
					for (int i = 0; i< doors.getLength(); i++) {
						Element auxDoor = (Element) doors.item(i);
						
						
						
						
						String id1 = auxDoor.getAttribute("id1");
						String id2 = auxDoor.getAttribute("id2");
						String result = auxDoor.getAttribute("result");
						
						System.out.println("AQUI,,,,,:::::"+id1+" "+id2+" "+result);
						combinacoes.add(new ObjectCombines(id1, id2, result));
						
					}
				}
			}
			
			
			/*
			Node combines_ = gameNode.getChildNodes().item(1);
			NodeList combines = combines_.getChildNodes();
			
			for (int combines_id = 0; combines_id < combines.getLength(); combines_id++) {

				Node nNode = combines.item(combines_id);

				if (nNode.getNodeType() == Node.ELEMENT_NODE) {
					Element eElement = (Element) nNode;
						String id1 = eElement.getAttribute("id1");
						String id2 = eElement.getAttribute("id2");
						String result = eElement.getAttribute("result");
						
						System.out.println("AQUI,,,,,:::::"+id1+" "+id2+" "+result);
						combinacoes.add(new ObjectCombines(id1, id2, result));
				}
			}
			
			*/
			
			System.out.println("____________________________dcadcd_____________________");
			
			
			
			//int value = Integer.parseInt(eElement.getAttribute("value"));
			//int combine = Integer.parseInt(eElement.getAttribute("combine"));
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Object[] ze = new Object[2];
		ze[0] = objectos;
		ze[1] = combinacoes;
		
		return ze;
	}
}
